package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.ProjectPlans;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.ProjectPlanVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import static com.zzyl.enums.BasicEnum.NURSINGLEVEL_BAN_FAIL;
import static com.zzyl.enums.BasicEnum.NURSINGPLAN_DELETE_FAIL;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    private static final Logger log = LoggerFactory.getLogger(NursingPlanServiceImpl.class);
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingPlan> listAll() {
        List<NursingPlan> list = nursingPlanMapper.listAll();
        return list;
    }

    @Transactional
    @Override
    public void addPlan(NursingPlan plan) {
        plan.setCreateTime(LocalDateTime.now());
        plan.setUpdateTime(LocalDateTime.now());
        plan.setStatus(1);
        plan.setCreateBy(1671403256519078153L);
        List<ProjectPlans> projectPlansList = plan.getProjectPlans();
        nursingPlanMapper.addNursingPlan(plan);
        Long id1 = plan.getId();
        Integer id2 = (int) id1.longValue();
        projectPlansList.stream().forEach(projectPlans -> {
            projectPlans.setPlanId(id2);
            projectPlans.setCreateTime(LocalDateTime.now());
            projectPlans.setUpdateTime(LocalDateTime.now());
        });
        nursingPlanMapper.addProjectPlans(projectPlansList);
    }

    @Override
    public PageResponse<NursingPlanVo> list(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>();
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.list(name, status);
        pageResponse.setRecords(page);
        pageResponse.setTotal(page.getTotal());
        pageResponse.setPages((long) page.getPages());
        pageResponse.setPage(page.getPageNum());
        return pageResponse;
    }

    @Override
    @Transactional
    public NursingPlanVo getNursingPlanById(Long id) {

        NursingPlanVo nursingPlanVo =nursingPlanMapper.getNursingPlanById(id);
        if(nursingPlanVo == null){ return null;}//或者抛出查找不到的异常

        //获取护理计划的项目集合
        List<ProjectPlanVo> projectPlanVos = nursingPlanMapper.getProjectPlans(id);
        if(projectPlanVos.isEmpty()){ return null;}
        nursingPlanVo.setProjectPlans(projectPlanVos);

        return nursingPlanVo;
    }

    @Override
    @Transactional
    public void updateNursingPlanById(NursingPlanVo nursingPlanVo) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanVo, NursingPlan.class);
        nursingPlanMapper.updateNursingPlanById(nursingPlan);

        if (nursingPlanVo.getProjectPlans().isEmpty())return;
        Long planId = nursingPlanVo.getId();
        Integer id = (int) planId.longValue();
        List<ProjectPlans> projectPlansList =nursingPlanVo.getProjectPlans().stream()
                .map(p->{
                    p.setPlanId(id);
                    ProjectPlans bean = BeanUtil.toBean(p, ProjectPlans.class);
                    return bean;
                })
                .collect(Collectors.toList());
        //删除原计划项目

        nursingPlanMapper.deleteProjectPlans(planId);
        //添加新计划项目
        nursingPlanMapper.addProjectPlans(projectPlansList);
    }

    @Override
    public void deleteById(Long id) {
        Integer i = nursingLevelMapper.getCount(id);
        if( i!=0) {
            throw new BaseException(NURSINGLEVEL_BAN_FAIL);

        }
        nursingPlanMapper.deleteById(id);
    }

    @Override
    public void ban(Long id, Integer status) {
        nursingPlanMapper.ban(id,status);
    }


}
